package com.ittest.bean;


import com.ittest.App;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


@Data
@ConditionalOnClass(name = "org.springframework.data.redis.core.RedisOperations")
@EnableConfigurationProperties(CartooProperties.class)
public class CartooCatAndMouse implements ApplicationContextAware {

    private Cat cat;
    private Mouse mouse;
    private CartooProperties cartooProperties;

    public CartooCatAndMouse(CartooProperties cartooProperties) {
        this.cartooProperties = cartooProperties;
        cat = new Cat();
        mouse = new Mouse();
        cat.setAge(cartooProperties.getCat() != null && cartooProperties.getCat().getAge() != null ? cartooProperties.getCat().getAge():3);
        cat.setName(cartooProperties.getCat() != null && StringUtils.hasText(cartooProperties.getCat().getName()) ? cartooProperties.getCat().getName() : "Tom");
        mouse.setAge(cartooProperties.getMouse() != null && cartooProperties.getMouse().getAge() != null ? cartooProperties.getMouse().getAge() :4);
        mouse.setName(cartooProperties.getCat() != null && StringUtils.hasText(cartooProperties.getMouse().getName()) ? cartooProperties.getMouse().getName() :"jerry");
    }

    public void play(){
        System.out.println(cat.getAge()+"岁的"+cat.getName()+"和"+mouse.getAge()+"岁的"+mouse.getName()+"打起来了");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 使用传入的应用上下文
        ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
        String[] beans = ctx.getBeanDefinitionNames();
        for (String bean : beans) {
            System.out.println(bean);
        }
    }
}
